package com.xuelang.pipeline.domain.task.service.impl;

import com.xuelang.pipeline.client.common.exception.BizException;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.task.dto.TaskQuery;
import com.xuelang.pipeline.domain.task.Task;
import com.xuelang.pipeline.domain.task.repository.TaskRepository;
import com.xuelang.pipeline.domain.task.service.TaskDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 流水线任务领域服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class TaskDomainServiceImpl implements TaskDomainService {

    @Autowired
    private TaskRepository taskRepository;

    @Override
    public PageRecord<Task> pageList(long currentPage, long pageSize, TaskQuery taskQuery) {
        return taskRepository.pageList(currentPage, pageSize, taskQuery);
    }

    @Override
    public List<Task> list(TaskQuery taskQuery) {
        return taskRepository.list(taskQuery);
    }

    @Override
    public Boolean save(Task task) {
        if(existsCode(task.getPipelineCode(), task.getCode())) {
            throw new BizException("任务编码已经存在");
        }
        if(existsName(task.getPipelineCode(), task.getCode(), task.getName())) {
            throw new BizException("任务名称已经存在");
        }
        return taskRepository.save(task);
    }

    @Override
    public Boolean update(Task task) {
        return taskRepository.update(task);
    }

    @Override
    public Boolean delete(String uuid) {
        return taskRepository.delete(uuid);
    }

    @Override
    public Task query(String uuid) {
        return taskRepository.query(uuid);
    }

    @Override
    public Boolean deleteByPipelineCode(String pipelineCode) {
        return taskRepository.deleteByPipelineCode(pipelineCode);
    }

    @Override
    public Boolean batchSaveByPipelineTemplateUuid(String pipelineTemplateUuid, String toPipelineCode, String userId) {
        return taskRepository.batchSaveByPipelineTemplateUuid(pipelineTemplateUuid, toPipelineCode, userId);
    }

    @Override
    public Boolean existsCode(String pipelineCode, String taskCode) {
        return taskRepository.existsCode(pipelineCode, taskCode);
    }

    @Override
    public Boolean existsName(String pipelineCode, String taskCode, String taskName) {
        return taskRepository.existsName(pipelineCode, taskCode, taskName);
    }
}
